// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dfa.axis.v1_16;

import com.google.api.ads.dfa.lib.client.DfaSession;
import com.google.api.ads.dfa.lib.factory.DfaServices;
import com.google.api.ads.dfa.axis.v1_16.CreativeAsset;
import com.google.api.ads.dfa.axis.v1_16.CreativeAssetSaveResult;
import com.google.api.ads.dfa.axis.v1_16.CreativeRemote;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * This example creates a HTML creative asset in a given advertiser. To create
 * an advertiser, run CreateAdvertiser.java.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: creative.saveCreativeAsset
 *
 * @author Joseph DiLallo
 */
public class CreateHTMLAsset {

  public static void main(String[] args) throws Exception {
    long advertiserId = Long.parseLong("INSERT_ADVERTISER_ID_HERE");
    String assetName = "INSERT_SWF_ASSET_NAME_HERE";
    String pathToFile = "INSERT_PATH_TO_SWF_FILE_HERE";

    // Initialize your session.
    DfaSession session = new DfaSession.Builder().fromFile().build();

    // Request the service.
    CreativeRemote service = new DfaServices().get(session, CreativeRemote.class);

    // Create HTML asset structure.
    CreativeAsset swfAsset = new CreativeAsset();
    swfAsset.setForHTMLCreatives(true);
    swfAsset.setAdvertiserId(advertiserId);
    swfAsset.setContent(getAssetDataFromFile(pathToFile));
    swfAsset.setName(assetName);

    // Create asset.
    CreativeAssetSaveResult creativeAssetSaveResult = service.saveCreativeAsset(swfAsset);

    // Display asset file name.
    System.out.println("Asset was saved with file name of \""
        + creativeAssetSaveResult.getSavedFilename() + "\".");
  }

  /**
   * Gets the asset data {@code byte[]} located in {@code fileName} or
   * {@code null} if the media could not be loaded.
   *
   * @param fileName the media file to load
   * @return the asset data {@code byte[]} located in {@code fileName}
   * @throws IOException if there is an problem loading the media data
   */
  public static byte[] getAssetDataFromFile(String fileName) throws IOException {
    BufferedInputStream inputStream = null;
    ByteArrayOutputStream outputStream = null;
    try {
      File imageFile = new File(fileName);
      inputStream = new BufferedInputStream(new FileInputStream(imageFile));
      outputStream = new ByteArrayOutputStream();
      int i = 0;
      while ((i = inputStream.read()) != -1) {
        outputStream.write(i);
      }
    } finally {
      if (inputStream != null) {
        inputStream.close();
      }
      if (outputStream != null) {
        outputStream.close();
      }
    }
    return (outputStream != null) ? outputStream.toByteArray() : null;
  }
}
